<template>
  <!-- 添加或修改检验标准对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs @tab-click="handleClick" active-name="factory_material_main">
        <el-tab-pane v-for="dict in dict.type.factory_test_item" :key="dict.value" :label="dict.label"
                     :name="dict.value">
          <el-table border v-loading="loading" :data="dataList">
            <el-table-column type="index" width="50" label="序号"></el-table-column>
            <el-table-column label="属性汉字" align="center" prop="propNameCh">
              <template slot-scope="scope">
                <el-input v-model="scope.row.propNameCh" size="mini"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="属性名称" align="center" prop="propName" width="150">
              <template slot-scope="scope">
                <el-input v-model="scope.row.propName" size="mini" disabled></el-input>
              </template>
            </el-table-column>

            <el-table-column label="合格结果" align="center" prop="result">
              <template slot-scope="scope">
                <el-radio-group v-model="scope.row.resultType">>
                  <el-radio
                    v-for="dict in resultTypeOptions"
                    :key="dict.value"
                    :label="dict.value"
                  >{{ dict.label }}
                  </el-radio>
                </el-radio-group>
              </template>
            </el-table-column>

            <el-table-column label="合格结果" align="center" prop="result">
              <template slot-scope="scope">
                <el-input v-model="scope.row.result" :disabled="scope.row.resultType !== '1'" size="mini"></el-input>
              </template>
            </el-table-column>

            <el-table-column label="最小值" align="center" prop="propValueMin">
              <template slot-scope="scope">
                <el-input-number v-model="scope.row.propValueMin" size="mini" :disabled="scope.row.resultType !== '0'" :precision="3"></el-input-number>
              </template>
            </el-table-column>
            <el-table-column label="最大值" align="center" prop="propValueMax">
              <template slot-scope="scope">
                <el-input-number v-model="scope.row.propValueMax" size="mini" :disabled="scope.row.resultType !== '0'" :precision="3"></el-input-number>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>


    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getAction, addAction, getById, updateAction} from '@/api/manage';
import moment from 'moment';

export default {
  name: "FactoryTestStandardForm",
  dicts: ['factory_test_item','factory_test_result_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        columns: [],
      },
      // 表单校验
      rules: {
        testType: [{required: true, message: "检验项目不能为空", trigger: "blur"}],
      },
      dataList: [],
      url: {
        getById: '/factoryTestStandard/factoryTestStandard/',
        batchInsert: '/factoryTestStandard/factoryTestStandard/batchInsert',
        edit: '/factoryTestStandard/factoryTestStandard/edit',
      },
      resultTypeOptions:[
        {label:"范围",value:"0"},
        {label:"描述",value:"1"},
      ]
    };
  },
  created() {

  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        goodsName: null,
        goodsCode: null,
        goodsModel: null,
        testType: null,
        propName: null,
        propNameCh: null,
        propValueMax: null,
        propValueMin: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检验标准";
    },
    /** 根据商品新增按钮操作 */
    handleAddByGoods(goods) {
      this.reset();
      this.open = true;
      this.form.goodsId = goods.id;
      this.form.goodsName = goods.name;
      this.form.goodsCode = goods.code;
      this.form.goodsModel = goods.model;
      this.handleClick({"name": "factory_material_main"}, {})
      this.title = "添加检验标准";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检验标准";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.testType == null || this.form.testType === "") {
        this.$modal.msgError("请选择检验类别");
        return;
      }
      this.form.columns = this.dataList;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.batchInsert, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },

    handleClick(tab, event) {
      this.dataList = [];
      this.loading = true;
      this.form.testType = tab.name;
      let goodsId = this.form.goodsId;
      getAction('/factoryTestStandard/factoryTestStandard/getTableColumns/' + tab.name + "/" + goodsId, {}).then(response => {
        this.loading = false;
        this.dataList = response.data;
      });
    }
  }
};
</script>
